﻿using System;
using CommonLibrary.Models.Profiles;

namespace CommonLibrary.ConfigurationManager.Helpers
{
    public static class ProfileHelpers
    {
        public static void GetProcessProfile(this ProcessedProfile processedProfile, string applicationName, ApplicationProfileConfig appProfile)
        {
            processedProfile.Configs.Clear();
            processedProfile.ProfileName = applicationName;
            processedProfile.ProfileDescription = appProfile.Description;

            foreach (CustomAction customAction in appProfile.ConfigActionsMap)
            {
                foreach (ActionEvent actionEvent in customAction.Trigger)
                {
                    try
                    {
                        processedProfile.ConfigAction.Add(actionEvent.Id,
                                                          new SepcialAction()
                                                              {
                                                                  ActionName = customAction.Name,
                                                                  Parameter = actionEvent.Parameter,
                                                                  Target = actionEvent.Target
                                                              });
                    }
                    catch
                    {
                    }
                }
            }

            foreach (Profile profile in appProfile.Profiles)
            {
                var processedConfig = new ProfileConfig();
                processedConfig.Id = profile.Id;

                foreach (CustomAction customAction in profile.ButtonsMap)
                {
                    foreach (ActionEvent actionEvent in customAction.Trigger)
                    {
                        try { processedConfig.ButtonActions.Add(actionEvent.Id, customAction.Name); }
                        catch (ArgumentException)
                        {
                            // Ignore duplicate keys this is not allowed 1 Key = 1 Action
                        }
                    }
                }

                foreach (CustomAction customAction in profile.AxisMap)
                {
                    foreach (ActionEvent actionEvent in customAction.Trigger)
                    {
                        switch (actionEvent.Type)
                        {
                            case TypeOfAction.Axis:
                                processedConfig.AxisAction.Add(actionEvent.Id, new AxisConfig
                                {
                                    ActionName = customAction.Name,
                                    DeadZone = actionEvent.DeadZone,
                                    Multiplier = actionEvent.Multiplier,
                                    Exponent = actionEvent.Exponenet,
                                    Shape = actionEvent.Shape
                                });
                                break;
                            case TypeOfAction.ButtonAxis:
                                processedConfig.ButtonActions.Add(actionEvent.Id, customAction.Name + "+");
                                processedConfig.ButtonActions.Add(actionEvent.OpositeId, customAction.Name + "-");
                                break;
                        }
                    }
                }

                foreach (CustomAction customAction in profile.SpecialActionsMap)
                {
                    foreach (ActionEvent actionEvent in customAction.Trigger)
                    {
                        try
                        {
                            processedConfig.SpecialActions.Add(actionEvent.Id,
                                                               new SepcialAction()
                                                                   {
                                                                       ActionName = customAction.Name,
                                                                       Parameter = actionEvent.Parameter,
                                                                       Target = actionEvent.Target
                                                                   });
                        }
                        catch (ArgumentException)
                        {
                            // Ignore duplicate keys this is not allowed 1 Key = 1 Action
                        }
                    }
                }
                processedProfile.Configs.Add(processedConfig);
            }
        }

    }
}
